const cv = require('opencv4nodejs');
const { grabFrames, drawRectAroundBlobs } = require('./utils');

const bgSubtractor = new cv.BackgroundSubtractorKNN();

const delay = 50;
grabFrames('rtsp://admin:HDMGLO@192.168.31.239:554/h264/ch1/main/av_stream', delay, (frame) => {
  const foreGroundMask = bgSubtractor.apply(frame);

  const iterations = 2;
  const dilated = foreGroundMask.dilate(
    cv.getStructuringElement(cv.MORPH_ELLIPSE, new cv.Size(4, 4)),
    new cv.Point(-1, -1),
    iterations
  );
  const blurred = dilated.blur(new cv.Size(10, 10));
  const thresholded = blurred.threshold(200, 255, cv.THRESH_BINARY);

  const minPxSize = 4000;
  drawRectAroundBlobs(thresholded, frame, minPxSize);

  //cv.imshow('foreGroundMask', foreGroundMask);
  cv.imshow('thresholded', thresholded);
  cv.imshow('frame', frame);
});
